﻿using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace ViewLED.UI
{
    /// <summary>
    /// Ptext.xaml 的交互逻辑
    /// </summary>
    public partial class Ptext : Page
    {
        int[] switchTime = new int[7] { 1, 2, 3, 5, 7, 10, 20 };
        ArrayList list = new ArrayList();
        bool mutex = true;
        public static DispatcherTimer timer = null;
        int s = 0;
        Storyboard story = new Storyboard();  //定义动画
        Screen scr;

        public Ptext()
        {
            InitializeComponent();
            cbxSpeed.SelectedIndex = int.Parse(ConfigurationManager.AppSettings["SwitchTime"]);
            getTxtFile();
            list = Model.FileExplorer.listFile("Text", true);
            if (list.Count > 0)
            {
                TextRange docTextRange = new TextRange(richTxt.Document.ContentStart, richTxt.Document.ContentEnd);
                using (FileStream fs = File.Open(list[0].ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                {
                    docTextRange.Load(fs, System.Windows.DataFormats.Rtf);
                    fs.Dispose();
                    fs.Close();
                    GC.Collect();
                }
                setAutoChangeTxt();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("请至少添加一个文件才能预览效果！", "错误提示");
            }
        }

        public void setAutoChangeTxt()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(switchTime[int.Parse(ConfigurationManager.AppSettings["SwitchTime"])]);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Interval = TimeSpan.FromSeconds(switchTime[int.Parse(ConfigurationManager.AppSettings["SwitchTime"])]);
            s = (s + 1) % list.Count;
            TextRange docTextRange = new TextRange(richTxt.Document.ContentStart, richTxt.Document.ContentEnd);
            using (FileStream fs = File.Open(list[s].ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
            {
                docTextRange.Load(fs, System.Windows.DataFormats.Rtf);
                story = (Storyboard)this.FindResource(Model.ScreenManager.randomAnimation());
                story.Begin(this);
                fs.Dispose();
                fs.Close();
                GC.Collect();
            }
        }

        // 获取文本文件
        private void getTxtFile()
        {
            cbxTxtList.ItemsSource = Model.FileExplorer.listFile("Text", false);
        }

        // 单独预览某一项
        private void cbxTxtList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mutex)
            {
                list.Clear();
                string p = System.AppDomain.CurrentDomain.BaseDirectory + "Text\\" + cbxTxtList.SelectedValue.ToString();
                if (File.Exists(p))
                {
                    list.Add(System.AppDomain.CurrentDomain.BaseDirectory + "Text\\" + cbxTxtList.SelectedValue.ToString());
                }
            }
        }

        private void playList(object sender, RoutedEventArgs e)
        {
            // 关闭已有的播放屏幕
            Model.ScreenManager.closeScreen();
            // 创建新屏幕
            scr = new Screen();
            scr.Show();
            scr.playText();
            // 对外播放时暂停预览界面的播放
            timer.Stop();
        }

        private void resetPlay(object sender, RoutedEventArgs e)
        {
            mutex = false;
            list = Model.FileExplorer.listFile("Text", true);
            getTxtFile();
            mutex = true;
        }

        private void quitPlay(object sender, RoutedEventArgs e)
        {
            Model.ScreenManager.closeScreen();
            // 退出对外播放后恢复预览界面的播放
            timer.Start();
        }

        // 创建文字文件
        private void addNewItem(object sender, RoutedEventArgs e)
        {
            Model.FileExplorer.createFile("Text\\" + DateTime.Now.ToString("yyyyMMddhhmmssffff"));
        }

        // 编辑某一条目的文件
        private void editItem(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "Text\\";
            fd.Filter = "写字板文件|*.rtf";
            fd.Multiselect = false;
            if (fd.ShowDialog() == DialogResult.OK && fd.FileName.IndexOf(fd.InitialDirectory) == 0)
            {
                Model.FileExplorer.editFile(fd.FileName);
            }
        }

        // 删除文字文件
        private void removeItem(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "Text\\";
            fd.Filter = "写字板文件|*.rtf";
            fd.Multiselect = false;
            if (fd.ShowDialog() == DialogResult.OK && fd.FileName.IndexOf(fd.InitialDirectory) == 0)
            {
                mutex = false;
                if (Model.FileExplorer.removeFile("Text\\" + fd.SafeFileName))
                {
                    list.Remove(fd.FileName);
                    getTxtFile();
                }
                mutex = true;
            }
        }

        // 修改速度设定
        private void cbxSpeed_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Model.FileExplorer.setConfig("SwitchTime", cbxSpeed.SelectedIndex.ToString());
        }

        // 添加已有文件
        private void moveInNewItem(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "写字板文件|*.rtf";
            fd.Multiselect = false;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                mutex = false;
                if (Model.FileExplorer.addFile(fd.FileName, System.AppDomain.CurrentDomain.BaseDirectory + "Text\\" + fd.SafeFileName))
                {
                    list.Add(fd.FileName);
                    getTxtFile();
                }
                mutex = true;
            }
        }
    }
}

